package com.ls.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

/**
 * <dl>
 * <dt>LocalCache</dt>
 * <dd>Description: 本地缓存，暂时只用于短信验证码</dd>
 * </dl>
 *
 * @author pengyu
 */
@Slf4j
@Component
public class LocalCache {

    /**缓存默认失效时间(分钟) */
    public static final long DEFAULT_TIME_MINUTE = 10;

    /**缓存清除动作执行间隔(秒)*/
    private static final long TASK_TIME = DEFAULT_TIME_MINUTE * 60;

    /**缓存默认失效时间(毫秒) */
    public static final long DEFAULT_TIMEOUT = TASK_TIME * 1000;

    /**缓存存储的map*/
    private static final ConcurrentHashMap<String,CacheEntity> CACHE_MAP = new ConcurrentHashMap<>(8);

    /**登录手机号前缀*/
    public static final String LOGIN_PREFIX = "login_";


    public LocalCache() {
    }

    private static LocalCache cache = null;

    public static LocalCache getInstance() {
    //单例一下
        if (cache == null) {
            cache = new LocalCache();
//            new Thread(new TimeoutTimer()).start();
        }

        return cache;
    }

//    @Scheduled(cron = "${schedule.validCode.cron}")
    /**从早上9点到23点，每隔1分钟执行1次定时任务 0 0/1 9-23 * * ? */
    public void refreshCache() {
//        log.info("refreshCache. start");
        for (String key: CACHE_MAP.keySet()) {
            CacheEntity entity = CACHE_MAP.get(key);
            long now = System.currentTimeMillis();
            if ((now - entity.getTimeStamp()) >= entity.getExpire()) {
                CACHE_MAP.remove(key);
            }
        }
//        log.info("refreshCache  end");
    }


    /**存储单元*/
    static class CacheEntity {

        /**值*/
        private Object value;

        /**过期时间(毫秒)*/
        private long expire;

        /**创建时的时间戳*/
        private long timeStamp;

        public Object getValue() {
            return value;
        }

        public void setValue(Object value) {
            this.value = value;
        }

        public long getExpire() {
            return expire;
        }

        public void setExpire(long expire) {
            this.expire = expire;
        }

        public long getTimeStamp() {
            return timeStamp;
        }

        public void setTimeStamp(long timeStamp) {
            this.timeStamp = timeStamp;
        }
    }

    public static boolean set(String key,Object value,long expire) {

        CACHE_MAP.put(key,setEntity(key,value,expire));

        return true;
    }

    public static boolean set(String key,Object value) {

        CACHE_MAP.put(key,setEntity(key,value,DEFAULT_TIMEOUT));

        return true;
    }

    private static CacheEntity setEntity(String key,Object value,long expire){

        CacheEntity entity = new CacheEntity();
        entity.setValue(value);
        entity.setExpire(expire);
        entity.setTimeStamp(System.currentTimeMillis());

        return entity;
    }

    public static Object get(String key) {
        CacheEntity entity = CACHE_MAP.get(key);

        if (entity == null) {
            return null;
        } else {
            Object value = entity.getValue();
            if (value == null) {
                return null;
            }
            return value;
        }
    }

    public static void remove(String key) {
        CACHE_MAP.remove(key);
    }

}